#!/usr/bin/perl

# $Id: 01_script_compiles.t 108 2010-06-17 12:48:15Z dolbanavt $
# $Date: 2010-06-17 15:48:19 +0300 (Чтв, 17 Июн 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/t/01_script_compiles.t $
# $LastChangedRevision: 350 $

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Const::Fast qw( const );
use Test::More;
use Test::Builder;
use IPC::Open3::Utils qw( put_cmd_in );

# configures behavior and functionality of put_cmd_in(). boolean arguments
const my $ARGS_FOR_OPEN3 => {
    ignore_handle          => 'stdout',
    close_stdin            => 1,
    carp_open3_errors      => 1,
    stop_read_on_open3_err => 1,
};

my $test = Test::Builder->new;

my @programs = glob catfile( $RealBin, '..', '*.pl' );

if ( @programs ) {
    plan tests => scalar @programs;

    foreach my $each_path_to_script ( @programs ) {
        pl_file_ok(
            $each_path_to_script,
            $each_path_to_script .' script compiles'
        );
    }
}
else {
    plan skip_all => 'no files in the selected directory';
}

sub pl_file_ok {
    my ( $path_to_file, $test_name ) = @_;

    unless ( -f $path_to_file ) {
        $test->ok( 0, $test_name );
        $test->note( $path_to_file .' does not exist' );

        return;
    }

    unless ( -x $path_to_file ) {
        $test->ok( 0, $test_name );
        $test->note( 'permission denied for '. $path_to_file );

        return;
    }

    my $test_cmd = join q{ }, (
        $EXECUTABLE_NAME,
        '-cw',
        $path_to_file,
    );

    put_cmd_in( $test_cmd, \my $stderr, $ARGS_FOR_OPEN3 );

    if ( $CHILD_ERROR ) {
        $test->ok( 0, 'script does not compile' );
        $test->note( $stderr );

        return;
    }
    else {
        $test->ok( 1, $test_name );

        return 1;
    }
}
